<template>
  <section>
    <span v-loading.fullscreen.lock="fullscreenLoading"> </span>
  </section>
</template>

<script>
export default {
  name: "dingLogin",
  data() {
    return {
      firstMenu: null, // 第一个菜单,
      token: "none",
      fullscreenLoading: false
    };
  },
  created() {
    // 如果临时登录吗存在，执行登录方法
    let code = this.$route.query.code;
    let state = this.$route.query.state;
    console.log(code, state);
    this.dingLoginAuth(code, state);
  },
  methods: {
    dingLoginAuth(code, state) {
      console.log(code, state, "dingLogin");
      this.fullscreenLoading = true;
      this.API.qrCodeLogin({ code: code, state: state })
        .then(res => {
          console.log(res.data, "token");
          this.token = res.data.value;
          window.sessionStorage.setItem("token", res.data.value);
          window.localStorage.setItem("clearInfo", "");
          this.getAllDic();
          this.getAllMenu();
        })
        .finally(() => {
          this.fullscreenLoading = false;
        });
    },
    // 获取用户菜单和按钮权限
    async getAllMenu() {
      await this.API.userResource().then(res => {
        console.log(res.data.menuLists);
        let tem = res.data;
        if (tem) {
          this.getWaitCount(tem.menuLists);
          this.$store
            .dispatch(
              "updateUserRoleKeys",
              tem.buttonLists && tem.buttonLists.length > 0
                ? tem.buttonLists.join(",")
                : ""
            )
            .then(res => {});
          sessionStorage.setItem(
            "roleKeys",
            tem.buttonLists && tem.buttonLists.length > 0
              ? tem.buttonLists.join(",")
              : ""
          );
          this.findFirstMenu(tem.menuLists);
          this.$store
            .dispatch("updateLeftMenu", tem.menuLists[0].children)
            .then(res => {
              this.$store
                .dispatch("updateLeftMenuIndex", this.firstMenu.id + "")
                .then(res => {});
            });
          this.$router.push(JSON.parse(this.firstMenu.route));
        }
      });
    },
    // 获取待办数据
    getWaitCount(menus) {
      this.API.getAllPrompt().then(res => {
        let tem;
        if (res.data) {
          console.log(res.data);
          menus.map(item => {
            this.setMenuCount(item, res.data);
          });
          tem = menus;
          console.log(tem, "count");
        } else {
          tem = menus;
        }
        this.$store.dispatch("updateUserMenus", tem).then(res => {});
      });
    },
    // 设置菜单的数量属性
    setMenuCount(item, data) {
      if (data) {
        data.map((waitItem, index) => {
          console.log(waitItem, index);
          if (waitItem.key === item.key) {
            this.$set(item, "count", waitItem.value);
          } else {
            if (item.children) {
              item.children.map(itemSub => {
                this.setMenuCount(itemSub, data);
              });
            }
          }
        });
      }
    },
    // 查找第一个菜单
    findFirstMenu(data) {
      if (data) {
        data.every(item => {
          console.log(item);
          if (
            (item.children === null || item.children.length === 0) &&
            item.menuType === "Menu"
          ) {
            if (this.firstMenu === null) {
              this.firstMenu = item;
            }
          } else {
            this.findFirstMenu(item.children);
          }
        });
      }
    },
    // 获取所有数据字典
    getAllDic() {
      this.API.dictionaryTree({ parentId: 0 }).then(res => {
        let tem = res.data;
        if (tem) {
          window.sessionStorage.setItem("allDic", JSON.stringify(tem));
        }
      });
    }
  }
};
</script>

<style scoped></style>
